<template>
  <div class="container">
    <div class="toolbar">
      <el-button @click="handleClickAddFirst" type="primary" size="mini" icon="el-icon-plus">添加课程体系</el-button>
      <el-button @click="handleClickEditFirstList" type="primary" size="mini" icon="el-icon-edit">编辑课程体系</el-button>
    </div>

    <vuedraggable class="tabs" v-model="list" @end="handleSort" animation="500">
        <div v-for="(item, index) in list" :key="item.id" class="item" :class="{'active':tabCurrentIndex === index}" @click="handleClickTab(index)">{{ item.name }}</div>
    </vuedraggable>
    <div class="add-class">
          <el-button type="primary" size="mini" @click="handleClickAddSecond">添加课程</el-button>
        </div>
        <template v-if="hasSubDada">
        <el-collapse v-model="activeCollName" @change="handleChangeCollapse" :accordion="true">
          <el-collapse-item v-for="(sub_item, i) in subList" :key="sub_item.id" style="position: relative;" :name="sub_item.id">
            <div class="toolbar-btn sub-edit"><span class="toolbar-btn" @click.stop="toGroup(sub_item)"
                v-if="!sub_item.children">编辑</span> <span @click="handleClickEditSecond(sub_item)">重命名</span><span
                @click="handleClickAddSub(sub_item)">添加下级</span><span @click="handleClickDeleteSecond(sub_item)">删除</span>
            </div>
            <template slot="title">
              <img class="sub-icon" :src="sub_item.course_image">{{ sub_item.name }}
            </template>
            <div class="class-list" v-if="sub_item.children">
              <div class="class-item" v-for="(child, k) in sub_item.children" :key="k"><span class="name">{{ k + 1 }}.
                  {{ child.name }}</span> <span class="toolbar-btn" @click.stop="toGroup(child)">编辑</span> <span
                  class="toolbar-btn" @click.stop="handleClickEditSecond(child)">重命名</span><span class="toolbar-btn"
                  @click.stop="handleClickDeleteSecond(child)">删除</span></div>
            </div>
            <div class="empty" v-else>暂无下级~</div>

          </el-collapse-item>
        </el-collapse>
        </template>
        <template  v-else><div class="empty">暂无数据~</div></template>

    <first-level-list v-if="showFirstList" @close="closeFirstList"></first-level-list>
    <first-level v-if="showFirst" @close="closeFirst" @success="firstSuccess"></first-level>
    <second-level v-if="showSecond" :showUpload="showUpload" :courseId="courseId" :type="secondType"
      @close="closeSecond" @commitAdd="secondAddCommit" @commitEdit="secondEditCommit"></second-level>



  </div>
</template>

<script>
import vuedraggable from "vuedraggable"
import FirstLevel from './components/FirstLevel.vue'
import FirstLevelList from './components/FirstLevelList.vue'
import SecondLevel from './components/SecondLevel.vue'
import { getCourseLevel, getCoursesList, addCourse, editCourse, deleteCourse,sortTabList  } from "@/api/course";
export default {
  components: {
    vuedraggable,
    FirstLevelList,
    FirstLevel,
    SecondLevel
  },
  data() {
    return {
      showFirst: false,
      showSecond: false,
      showFirstList: false,
      activeCollName: '',
      list: [],
      tabId: '',
      subList: [],
      secondType: '',
      courseId: '',
      pid: '',
      showUpload: true,
      tabCurrentIndex:0
    }
  },
  computed:{
    hasSubDada(){
      return this.subList.length >0
    }
  },
  created() {
    this.activeCollName = localStorage.getItem('activeCollName') !== null ? localStorage.getItem('activeCollName')*1 : 0;
    this.tabCurrentIndex = localStorage.getItem('tabCurrentIndex') !== null ? localStorage.getItem('tabCurrentIndex')*1 : 0;
    this.getList();
  },
  methods: {
    async getList() {
      let res = await getCourseLevel();
      this.list = res.data;
      this.tabId = res.data[this.tabCurrentIndex].id;
      this.getSubList();
    },
    async getSubList() {
      let res = await getCoursesList({ course_level_id: this.tabId });
      this.subList = res.data;
    },
    async addCourse(form) {
      let res = await addCourse(form);
      this.$message({
        message: '添加成功！',
        type: 'success'
      });
      this.closeSecond();
      this.getSubList();
    },
    async editCourse(form) {
      let res = await editCourse(form);
      this.$message({
        message: '编辑成功！',
        type: 'success'
      });
      this.closeSecond();
      this.getSubList();
    },
    async deleteCourse(id) {
      let res = await deleteCourse({ id });
      this.$message({
        message: '删除成功！',
        type: 'success'
      });
      this.getSubList();
    },
    async sortTabList(sort_ids){
      let res = await sortTabList({ sort_ids });
      this.tabCurrentIndex = 0;
      this.getList();
    },
    handleSort() {
      let ids = [];
      this.list.forEach(item=>{
        ids.push(item.id)
      })
      this.sortTabList(ids.join(','));
    },
    toEdit(item) {
      this.$router.push({
        path: '/exercises/group',
        query: {
          id: item.id
        }
      })
    },
    toGroup(item) {
      this.$router.push({
        path: '/exercises/group',
        query: {
          id: item.id
        }
      })
    },
    handleClickTab(index) {
      this.tabId = this.list[index].id;
      this.tabCurrentIndex = index;
      localStorage.setItem('tabCurrentIndex',index);
      this.getSubList()
    },
    handleChangeCollapse(activeNames) {
      localStorage.setItem('activeCollName',activeNames)
    },
    handleClickAddFirst() {
      this.showFirst = true;
    },
    handleClickEditFirstList(item) {
      this.showFirstList = true;
    },
    isShowUpload() {
      if (this.pid == 0) {
        this.showUpload = true
      } else {
        this.showUpload = false
      }
    },
    handleClickAddSecond() {
      this.secondType = 'add';
      this.pid = '0';
      this.isShowUpload();
      this.showSecond = true;
    },

    handleClickEditSecond(item) {
      console.log(item)
      this.secondType = 'edit';
      this.courseId = item.id;
      this.pid = item.pid;
      this.isShowUpload();
      this.showSecond = true;

    },
    handleClickAddSub(item) {
      this.secondType = 'add';
      this.pid = item.id;
      this.isShowUpload();
      this.showSecond = true;

    },
    handleClickDeleteSecond(item) {
      this.$confirm('确定删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteCourse(item.id)
      })
    },
    closeFirst() {
      this.showFirst = false;
    },
    firstSuccess() {
      this.showFirst = false;
      this.getList();
    },
    closeFirstList() {
      this.showFirstList = false;
      this.getList();
    },
    closeSecond() {
      this.showSecond = false;
    },
    secondAddCommit(form) {
      form['course_level_id'] = this.tabId;
      form['pid'] = this.pid;
      this.addCourse(form);
    },
    secondEditCommit(form) {
      form['course_level_id'] = this.tabId;
      form['id'] = this.courseId;
      form['pid'] = this.pid;
      this.editCourse(form);
    },

  },
}
</script>
<style lang="scss" scoped>
.container {
  padding: 20px;
}

.toolbar {
  background-color: #fff;
  margin-bottom: 20px;
}

.sub-icon {
  width: 30px;
  height: 20px;
  margin-right: 10px;
  vertical-align: middle;
}

.sub-edit {
  position: absolute;
  top: 11px;
  right: 40px;
  z-index: 1;

  span {
    margin-left: 6px
  }
}

.el-collapse {
  position: relative;
}

.add-class {
  margin-bottom: 15px;
  text-align: right;
}

.empty {
  margin-left: 40px;
  padding: 15px;
  text-align: center;
}

.class-list {
  margin-left: 40px;
  padding: 15px;
  background-color: #eee;

  .class-item {
    margin-bottom: 10px;

    .name {
      cursor: pointer;
    }

    .name:hover {
      color: #409EFF
    }
  }

  .class-item:last-child {
    margin-bottom: 0;
  }

}

.el-icon-edit-outline {
  color: #409EFF;
  cursor: pointer;
  font-size: 16px
}


.tabs{
  width:100%;
  overflow-y:auto;
  .item{
    padding: 0 20px;
    height: 40px;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    line-height: 40px;
    display: inline-block;
    list-style: none;
    font-size: 14px;
    font-weight: 500;
    color: #303133;
    position: relative;
    cursor: pointer;
  }
  .item.active{
    border-bottom:2px solid #409EFF
  }
}
</style>
